package com.wealth.scsj.p_app.view.activity.loginmodel;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.vondear.rxtools.RxDataTool;
import com.wealth.scsj.p_app.R;
import com.wealth.scsj.p_app.utils.RxCountDown;
import com.wealth.scsj.p_app.utils.SkipUtils;
import com.wealth.scsj.p_app.utils.Tool;
import com.wealth.scsj.p_app.view.activity.BaseActivity;


import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity implements  View.OnClickListener {

    private ImageView mRegisterCancelBtn;
    /**  */
    private EditText mRegisterPhoneEt;
    /**
     * 发送验证码
     */
    private Button mSendCodeBtn;
    /**  */
    private EditText mRegisterCodeEt;
    /**  */
    private EditText mRegisterPswEt;
    private ImageView mScanIv;
    /**  */
    private EditText mTuijianCodeEt;
    private CheckBox mCheckbox;
    /**
     * 【商超世界注册协议】
     */
    private TextView mXieyi;
    /**
     * 注册
     */
    private Button mRegisterBtn;
    /**
     * 立即登录
     */
    private TextView mLoginRightNow;
    private String mSRegisterPsw;
    private String mSRegisterPhone;
    private String mSRegisterCode;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register_layout);
        initView();

    }

    @Override
    protected void initView() {

        mRegisterCancelBtn = (ImageView) findViewById(R.id.register_cancel_btn);
        mRegisterPhoneEt = (EditText) findViewById(R.id.register_phone_et);
        mSendCodeBtn = (Button) findViewById(R.id.send_code_btn);
        mRegisterCodeEt = (EditText) findViewById(R.id.register_code_et);
        mRegisterPswEt = (EditText) findViewById(R.id.register_psw_et);
        mScanIv = (ImageView) findViewById(R.id.scan_iv);
        mTuijianCodeEt = (EditText) findViewById(R.id.tuijian_code_et);
        mCheckbox = (CheckBox) findViewById(R.id.checkbox);
        mXieyi = (TextView) findViewById(R.id.xieyi);
        mRegisterBtn = (Button) findViewById(R.id.register_btn);
        mLoginRightNow = (TextView) findViewById(R.id.login_right_now);
    }

    @Override
    protected void initListener() {
        mRegisterCancelBtn.setOnClickListener(this);
        mSendCodeBtn.setOnClickListener(this);
        mScanIv.setOnClickListener(this);
        mXieyi.setOnClickListener(this);
        mRegisterBtn.setOnClickListener(this);
        mLoginRightNow.setOnClickListener(this);

    }

    @Override
    protected void initData() {

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.register_cancel_btn://有上角关闭
                finish();
                break;
            case R.id.send_code_btn://发送验证码

                mSRegisterPhone = mRegisterPhoneEt.getText().toString().trim();
                if (Tool.isPhoneNumber(mSRegisterPhone)) {

                    RxCountDown.countdown(60)
                            .doOnSubscribe(new Action0() {
                                @Override
                                public void call() {
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("phone", mSRegisterPhone);
                                    jsonObject.put("relationType", "registration");
                                    jsonObject.put("second", 300);//过期时间
                                    jsonObject.put("templateCode", "SMS_124800108");//短信模版编号
                                    RequestBody requestBody = RequestBody.create(okhttp3.MediaType
                                            .parse("application/json;charset=utf-8"), jsonObject.toString());
                                    //smsPresenter.deliverData(requestBody);
                                    mSendCodeBtn.setClickable(false);
                                }
                            })
                            .subscribe(new Subscriber<Integer>() {
                                @Override
                                public void onCompleted() {
                                    mSendCodeBtn.setClickable(true);
                                    mSendCodeBtn.setText("发送验证码");
                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(Integer integer) {
                                    mSendCodeBtn.setText(integer + "s");
                                }
                            });


                } else {
                    Toasty.info(this, "请输入正确的手机号").show();
                }

                break;
            case R.id.scan_iv://推荐人右边的二维码图标

                break;
            case R.id.xieyi://协议

                break;
            case R.id.register_btn://注册按钮
                mSRegisterPhone = mRegisterPhoneEt.getText().toString().trim();
                mSRegisterCode = mRegisterCodeEt.getText().toString().trim();
                mSRegisterPsw = mRegisterPswEt.getText().toString().trim();
                if (!Tool.isPhoneNumber(mSRegisterPhone)) {
                    Toasty.info(this, "请输入合法的手机号").show();
                    return;
                }
                if (!(RxDataTool.isEmpty(mSRegisterPhone)) && !(RxDataTool.isEmpty(mSRegisterCode)) && !(RxDataTool.isEmpty(mSRegisterPsw))) {

                    closeKeyboard();
//                    LoginRequestBean login = new LoginRequestBean();
//                    login.setUsername(mSPhoneEt1);
//                    login.setPassword(mSPswEt);
//                    login.setCodeFromWeixin(null);
//                    login.setClientType(3);
//                    Gson gson = new Gson();
//                    String obj = gson.toJson(login);
//                    loginPresenter.deliverList(this, body);

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("username", mSRegisterPhone);
                    jsonObject.put("password", mSRegisterPsw);
                    jsonObject.put("smscode", mSRegisterCode);
                    RequestBody body = RequestBody.create(okhttp3.MediaType
                            .parse("application/json;charset=utf-8"), jsonObject.toString());
                    //presenter.deliverData(this, body);
                } else {

                    Toasty.error(this, "请完善账号信息").show();
                }
                break;
            case R.id.login_right_now://跳转到登录
                SkipUtils.toLogin(this);
                break;
        }
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


}
